class GameScene extends eui.Component implements  eui.UIComponent {
	private rect1:eui.Image;
	private rect2:eui.Image;
	private yi:eui.Image[];
	private er:eui.Image[];
	private btnstr:eui.Image;
	private wanjia:player;
	private car:eui.Image;
	private leftx:number=60;
	private centerx:number=260;
	private rightx:number=460;
	private btnL:btnleft;
	private btnR:btnright;
	private diren:string[];
	private create:eui.Image[];
	private btnjump:eui.Image;
	private speed:number=0;
	private direnmovespeed:number=8
	private removehit:number=0;
	private df:number=0;
	private maxdf:number;
	private showdf:eui.Label;
	private jieguo:Showjg;
	private sctime:number=0;
	private timer:egret.Timer=new egret.Timer(1000);
	private btnjump2:eui.Image;
	public constructor() {
		super();
	   this.maxdf=JSON.parse(localStorage.getItem("df"))
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}


	protected childrenCreated():void
	{  this.yi=[]
		this.er=[]
        this.diren=[]
		this.create=[];
		this.btnjump2.visible=false;
		this.diren=["diren1_png","diren2_png"]	
		this.rect1=new eui.Image();
		this.rect2=new eui.Image();
        this.rect1.source=RES.getRes("a1_png");
		this.rect1.x=200;
		this.rect2.source=RES.getRes("a2_png");
		this.rect2.x=400;
		this.yi.push(this.rect1) 
		this.er.push(this.rect2)
		this.addChildAt(this.rect1,1)
		this.addChildAt(this.rect2,1)
	    this.btnstr=new eui.Image;
	    this.btnstr.source=RES.getRes("btnstr_png");
		this.btnstr.touchEnabled=true;
		this.btnstr.width=200
		this.btnstr.height=100
		this.btnstr.x=210;
		this.btnstr.y=600;
		this.addChild(this.btnstr)
		this.btnstr.addEventListener(egret.TouchEvent.TOUCH_TAP,this.gamestart,this)
		super.childrenCreated();
		this.gameinit();
	}

	private gameinit(){
		 		this.car=new eui.Image();
				 this.car.source=RES.getRes("player_png")
				 this.car.x=this.centerx;
				 this.car.y=1136-200
				 this.addChildAt(this.car,5)
		}
	private gamestart(){
		this.removeChild(this.btnstr)
		this.addEventListener(egret.Event.ENTER_FRAME,this.bgroll,this)
	    this.btnL.touchEnabled=true
		this.btnR.touchEnabled=true
		this.btnjump.touchEnabled=true
		this.addChildAt(this.btnL,6)
		this.addChildAt(this.btnR,6)
		this.addChildAt(this.btnjump,6)
		this.btnL.addEventListener(egret.TouchEvent.TOUCH_TAP,this.btnlevent,this)
		this.btnR.addEventListener(egret.TouchEvent.TOUCH_TAP,this.btnrevent,this)
        this.btnjump.addEventListener(egret.TouchEvent.TOUCH_TAP,this.jumpevent,this)
		this.btnjump2.addEventListener(egret.TouchEvent.TOUCH_TAP,this.jump2event,this)
        this.timer.addEventListener(egret.TimerEvent.TIMER,this.creatediren,this);
		this.timer.start();
		this.addEventListener(egret.Event.ENTER_FRAME,this.direnmove,this);
		
	}
    private jump2statu:number=0;
	private jump2event(){
       this.jump2statu=1
	}
    private btnlevent(e:TouchEvent){
         if(this.car.x==this.centerx){
		 this.car.x=this.leftx;}
		 if(this.car.x==this.rightx){
			 this.car.x=this.centerx;
		 }
		
	}
	private jumpevent(){
		//this.btnL.touchEnabled=false
		//this.btnR.touchEnabled=false
		this.btnjump.touchEnabled=false
		
		//this.addEventListener(egret.Event.ENTER_FRAME,this.carjump,this)
		this.addEventListener(egret.Event.ENTER_FRAME,this.carjump,this)
	}
	private carjump(){
    if(this.jump2statu==0){
		if(this.speed<35){
			 this.btnjump2.visible=true;
		this.btnjump2.touchEnabled=true;
			this.car.width+=2
		 this.car.y-=2
		 this.car.height+=2
		 this.car.x-=2
		}

		 if(this.speed>=35){
	    
		this.btnjump2.touchEnabled=false
		this.btnjump2.visible=false
		this.car.width-=2
		 this.car.y+=2
		 this.car.height-=2
		 this.car.x+=2
		 }
		  this.speed+=1
           this.removehit=1;
		
		if(this.speed>=70){
			this.removeEventListener(egret.Event.ENTER_FRAME,this.carjump,this)
			this.speed=0
			this.removehit=0
			 this.btnL.touchEnabled=true
		this.btnR.touchEnabled=true
		this.btnjump.touchEnabled=true
		this.btnjump2.visible=false
		}
	}
	 else if(this.jump2statu==1){
		if(this.speed<60){
			this.car.width+=2
		 this.car.y-=2
		 this.car.height+=2
		 this.car.x-=2
		}
		 if(this.speed>=60){
		this.car.width-=2
		 this.car.y+=2
		 this.car.height-=2
		 this.car.x+=2
		 }
		  this.speed+=1
           this.removehit=1;
		
		if(this.speed>=120){
			this.removeEventListener(egret.Event.ENTER_FRAME,this.carjump,this)
			this.speed=0
			this.removehit=0
			this.jump2statu=0
			 this.btnL.touchEnabled=true
		this.btnR.touchEnabled=true
		this.btnjump.touchEnabled=true
		this.btnjump2.touchEnabled=false
		this.btnjump2.visible=false
	 }}
		console.log(2333)
	}
	private hittest(obj1:egret.DisplayObject,obj2:egret.DisplayObject){
          let rect1:egret.Rectangle=obj1.getBounds();
		let rect2:egret.Rectangle=obj2.getBounds();
		rect1.x=obj1.x
		rect1.y=obj1.y
		rect2.x=obj2.x
		rect2.y=obj2.y
		if(rect1.intersects(rect2)){
			//this.removeEventListener(egret.Event.ENTER_FRAME,this.js,this);
			this.removeEventListener(egret.Event.ENTER_FRAME,this.bgroll,this)
			this.removeEventListener(egret.Event.ENTER_FRAME,this.direnmove,this);
			this.timer.stop();
			this.gameover();
			console.log("头和四周发生了碰撞，停止计时器，调用gameover方法")
		}
	}
	private gameover(){
		this.btnL.touchEnabled=false
		this.btnR.touchEnabled=false
		this.btnjump.touchEnabled=false
		if(this.df>this.maxdf){
		let a=JSON.stringify(this.df)
		localStorage.setItem("df",a)
		}
		this.btnL.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.btnlevent,this)
		this.btnR.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.btnrevent,this)
		this.jieguo=new Showjg();
		this.jieguo.setdf(this.df)
		this.addChildAt(this.jieguo,this.numChildren+1);
		
	}
	private btnrevent(){
		if(this.car.x==this.centerx){
		this.car.x=this.rightx}
		if(this.car.x==this.leftx){
			this.car.x=this.centerx;
		}
	}
	private bgroll(){
          for(let a=0;a<this.yi.length;a++){
			  this.yi[a].y+=4;
			  if(this.yi[a].y==4){
				  this.yi.push(this.product(200))
			  }
		  }
		  for(let a=0;a<this.er.length;a++){
			  this.er[a].y+=4;
			  if(this.er[a].y==4){
				  this.er.push(this.product(400))
			  }
		  }
		  this.sctime+=1;
		  if(this.sctime%10==0){
			this.df+=1
			this.showdf.text="得分："+JSON.stringify(this.df)
		}
	}

	private product(aa:number){
		let a:eui.Image;
		a=new eui.Image;
		a.x=aa;
		a.y=-1236;
		//a.width=200;
		//a.height=1136
		let b=Math.ceil(Math.random()*2)
		if(b==1){
		a.source=RES.getRes("a1_png")
		}else{
			a.source=RES.getRes("a2_png")
		}
	    this.addChildAt(a,1)
		return a
	}
	private creatediren(){
		let a=Math.ceil(Math.random()*3)
		let b=Math.ceil(Math.random()*2)
		let c:eui.Image;
		c=new eui.Image();
		c.y=-200;
	  if(a==1){
		  if(b==1){
			  c.source=this.diren[0];
			  c.x=this.leftx;
		  }
		  if(b==2){
			  c.source=this.diren[1];
			  c.x=this.leftx;
		  }
	  }
	  if(a==2){
		  if(b==1){
			  c.source=this.diren[0];
			  c.x=this.centerx;
		  }
		  if(b==2){
			  c.source=this.diren[1];
			  c.x=this.centerx;
		  }
	  }
	  if(a==3){
		  if(b==1){
			  c.source=this.diren[0];
			  c.x=this.rightx;
		  }
		  if(b==2){
			  c.source=this.diren[1];
			  c.x=this.rightx;
		  }
	  }
    this.create.push(c)
	  this.addChildAt(c,1)
	}
	private direnmove(){
		if(this.df>=50&&this.df<100){this.direnmovespeed=10}
		if(this.df>=100&&this.df<150){this.direnmovespeed=12}
		if(this.df>=150&&this.df<200){this.direnmovespeed=14}
		for(let a=0;a<this.create.length;a++){
			this.create[a].y+=this.direnmovespeed
			if(this.removehit==0){
			this.hittest(this.create[a],this.car)
			}
		}
		
	}
	
}